package com.hq.shop08goods.controller;

import com.hq.commons.common.R;
import com.hq.commons.ov.Goods;
import com.hq.shop08goods.entity.GoodsEntity;
import com.hq.shop08goods.service.GoodsService;
import com.hq.shop08goods.service.StockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private StockService stockService;

    @GetMapping("getGoods")
    public List<GoodsEntity> getGoodsList() {
        return goodsService.list();
    }

    @GetMapping("info/{id}")
    public GoodsEntity info(@PathVariable Integer id) {
        return goodsService.getById(id);
    }

    @PostMapping("addGood")
    public GoodsEntity addGood(@RequestBody GoodsEntity goods) {
        boolean save = goodsService.save(goods);
        return goods;
    }

    @PostMapping("updateGoods")
    public R updateGoods(@RequestBody GoodsEntity goods) {
        return R.write(goodsService.updateById(goods));
    }

    @GetMapping("deleteGoods/{id}")
    public R deleteGoods(@PathVariable Integer id) {
        return R.write(goodsService.removeById(id));
    }

    @PostMapping("save")
    public Goods addGoodAndStock(@RequestBody Goods goods) {
        return goodsService.addGoodAndStock(goods);
    }
}
